﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fabrica
{
    public partial class EntregaProductos : Form
    {
        FabricaEntities entities = new FabricaEntities();
        VentaNodo1.VentaEntities entitiesVenta = new VentaNodo1.VentaEntities();
        VentaNodo2.VentaEntities1 entitiesVenta2 = new VentaNodo2.VentaEntities1();
        List<Delivery> lista = new List<Delivery>();

        public EntregaProductos()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            entities.AddToStores(
                new Store
                {
                    storeId = Convert.ToInt32(textBox1.Text),
                    name = textBox2.Text,
                    ubication = textBox3.Text
                });

            if (Convert.ToInt32(textBox1.Text) % 2 == 0) // Tiendas pares en un nodo, tiendas impares en otro nodo :D
            {
                entitiesVenta.AddToStores(
                    new VentaNodo1.Store
                    {
                        storeId = Convert.ToInt32(textBox1.Text),
                        name = textBox2.Text,
                        ubication = textBox3.Text
                    });
            }
            else
            {
                entitiesVenta2.AddToStores(
                    new VentaNodo2.Store
                    {
                        storeId = Convert.ToInt32(textBox1.Text),
                        name = textBox2.Text,
                        ubication = textBox3.Text
                    });
            }
            entities.SaveChanges();
            entitiesVenta.SaveChanges();
            entitiesVenta2.SaveChanges();
            MessageBox.Show("Tienda Agregada con exito");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Delivery item in lista)
            {
                entities.AddToDeliveries(item);
                if (item.storeid % 2 == 0)
                {
                    entitiesVenta.AddToDeliveries(new VentaNodo1.Delivery
                    {
                        deliveryId = item.deliveryId,
                        deliveryDate = item.deliveryDate,
                        quantity = item.quantity,
                        storeid = item.storeid,
                        toyid = item.toyId
                    });
                }
                else
                {
                    entitiesVenta2.AddToDeliveries(new VentaNodo2.Delivery
                    {
                        deliveryId = item.deliveryId,
                        deliveryDate = item.deliveryDate,
                        quantity = item.quantity,
                        storeid = item.storeid,
                        toyid = item.toyId
                    });
                }
            }
            entities.SaveChanges();
            entitiesVenta.SaveChanges();
            entitiesVenta2.SaveChanges();
            MessageBox.Show("Detalle de entrega Agregada con exito");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lista.Add(new Delivery
            {
                deliveryId = (from d in entities.Deliveries
                              select d).Count() + lista.Count() + 1,
                deliveryDate = DateTime.Today,
                quantity = Convert.ToInt32(textBox5.Text),
                storeid = Convert.ToInt32(textBox1.Text),
                toyId = (from t in entities.Toys
                         where t.name == textBox4.Text
                         select t).FirstOrDefault().toyId
            });
            MessageBox.Show("Entrega Agregada con exito");
        }
    }
}
